package com.yf.zzmusic.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.date.DateUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.exception.ServiceException;
import com.yf.zzmusic.pojo.dto.music.MusicPageDTO;
import com.yf.zzmusic.pojo.dto.music.MusicUpdateDTO;
import com.yf.zzmusic.pojo.entity.Music;
import com.yf.zzmusic.mapper.MusicMapper;
import com.yf.zzmusic.pojo.entity.User;
import com.yf.zzmusic.service.IMusicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.zzmusic.utils.GetMusicTimer;
import com.yf.zzmusic.utils.Image2Binary;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 音乐表，如果对应的音乐被删除，其系统中的mp3源文件也应该被删除 服务实现类
 * </p>
 *
 * @author
 * @since 2024-02-04
 */
@Service
public class MusicServiceImpl extends ServiceImpl<MusicMapper, Music> implements IMusicService {

  @Override
  public PageInfo musicByPage(MusicPageDTO musicPageDTO) {
    QueryWrapper<Music> queryWrapper = new QueryWrapper<>();
    Integer pageNum = musicPageDTO.getPageNum();
    Integer pageSize = musicPageDTO.getPageSize();
    String name = musicPageDTO.getName();
    String singerName = musicPageDTO.getSingerName();
    Integer singerId = musicPageDTO.getSingerId();
    String albumName = musicPageDTO.getAlbumName();
    Integer albumId = musicPageDTO.getAlbumId();
    Integer createUserId = musicPageDTO.getCreateUserId();
    Integer areaType=musicPageDTO.getAreaType();
    Integer type = musicPageDTO.getType();
    queryWrapper.orderByDesc("send_timer");
    // 判断pageNum与pageSize
    if(pageNum==null){
      pageNum=0;
    }
    if(pageSize==null){
      pageSize=1000;
    }
    if (!StringUtils.isEmpty(name)) {
      queryWrapper.like("name", name);
    }
    if (!StringUtils.isEmpty(singerName)) {
      queryWrapper.like("singer_name", singerName);
    }
    if (singerId!=null) {
      queryWrapper.eq("singer_id", singerId);
    }
    if (!StringUtils.isEmpty(albumName)) {
      queryWrapper.like("album_name", albumName);
    }
    if (albumId!=null){
      queryWrapper.eq("album_id",albumId);
    }
    if (createUserId!=null){
      queryWrapper.eq("create_user_id",createUserId);
    }
    if (areaType!=null){
      queryWrapper.eq("area_type", areaType);
    }
    if (type!=null) {
      queryWrapper.eq("type", type);
    }
    Page<Music> musicPage = page(new Page<>(pageNum, pageSize), queryWrapper);
    PageInfo<Music> pageInfo=new PageInfo<>();
    pageInfo.setPageNum(pageNum);
    pageInfo.setPageSize(pageSize);
    pageInfo.setTotal(musicPage.getTotal());
    pageInfo.setData(musicPage.getRecords());
    return pageInfo;
  }

  @Override
  public boolean updateMusic(MusicUpdateDTO musicUpdateDTO) {
    if (getById(musicUpdateDTO.getMusicId())==null){
      throw new ServiceException(Constants.NO_CONTENT,"音乐未找到");
    }
    Music music=new Music();
    //拷贝
    BeanUtils.copyProperties(musicUpdateDTO,music);
    return updateById(music);
  }

  @Override
  public boolean insertMusic(Music music) {
    try {
      String tempPath=GetMusicTimer.getFileByUrl(music.getUrl());
      music.setTimer((int)(GetMusicTimer.getDuration(tempPath)));
      return save(music);
    } catch (IOException e) {
      throw new ServiceException(400,"系统错误");
    }
  }

  @Override
  public List<Music> musicByIds(String ids) {
    if (ids==null){
      throw new ServiceException(400,"缺少ids必填字段");
    }
    List<String> strList=Arrays.stream(ids.split(",")).collect(Collectors.toList());
    return listByIds(strList);
  }
}
